package marmalius;
/*
	Effectue la gestion des vendeurs
	modifie par Metos [EOAH]
*/

import java.sql.*;

import marmalius.tools.DBService;
import marmalius.tools.LogService;

public class ShopHandler {
	
	private final int MaxShops = 101; // 1 more because we don't use [0] !
	private int MaxShopItems = 101; // 1 more because we don't use [0] !
	//public static int MaxInShopItems = 40;
	private final int MaxShowDelay = 60;
	private int TotalShops = 0;
	private int[][] ShopItems = new int[MaxShops][MaxShopItems];
	private int[][] ShopItemsN = new int[MaxShops][MaxShopItems];
	private int[][] ShopItemsDelay = new int[MaxShops][MaxShopItems];
	private int[][] ShopItemsSN = new int[MaxShops][MaxShopItems];
	private int[] ShopItemsStandard = new int[MaxShops];
	private String[] ShopName = new String[MaxShops];
	private int[] ShopSModifier = new int[MaxShops];
	private int[] ShopBModifier = new int[MaxShops];

	public ShopHandler () {
		for (int i = 0; i < MaxShops; i++) {
			for (int j = 0; j < MaxShopItems; j++) {
				ResetItem(i, j);
				ShopItemsSN[i][j] = 0;
			}
			ShopItemsStandard[i] = 0;
			ShopSModifier[i] = 0;
			ShopBModifier[i] = 0;
			ShopName[i] = "";
		}
		TotalShops = 0;
		loadShops();
	}

	public void process () {
		boolean DidUpdate = false;
		for (int i = 1; i <= TotalShops; i++) {
			for (int j = 0; j < MaxShopItems; j++) {
				if (ShopItems[i][j] > 0) {
					if (ShopItemsDelay[i][j] >= MaxShowDelay) {
						if (j <= ShopItemsStandard[i] && ShopItemsN[i][j] <= ShopItemsSN[i][j]) {
							if (ShopItemsN[i][j] < ShopItemsSN[i][j]) {
								ShopItemsN[i][j] += 1;
							}
						}
						else {
							DiscountItem(i, j);
						}
						ShopItemsDelay[i][j] = 0;
						DidUpdate = true;
					}
					ShopItemsDelay[i][j]++;
				}
			}
			if (DidUpdate == true) {
				for (int k = 1; k < PlayerHandler.getInstance().getMaxPlayers(); k++) {
					if (PlayerHandler.getInstance().getPlayer(k) != null) {
						if (PlayerHandler.getInstance().getPlayer(k).IsShopping == true && PlayerHandler.getInstance().getPlayer(k).MyShopID == i) {
							PlayerHandler.getInstance().getPlayer(k).UpdateShop = true;
						}
					}
				}
				DidUpdate = false;
			}
		}
	}

	public void DiscountItem (int ShopID, int ArrayID) {
		ShopItemsN[ShopID][ArrayID] -= 1;
		if (ShopItemsN[ShopID][ArrayID] <= 0) {
			ShopItemsN[ShopID][ArrayID] = 0;
			ResetItem(ShopID, ArrayID);
		}
	}
	
	public void ResetItem (int ShopID, int ArrayID) {
		ShopItems[ShopID][ArrayID] = 0;
		ShopItemsN[ShopID][ArrayID] = 0;
		ShopItemsDelay[ShopID][ArrayID] = 0;
	}

	public boolean loadShops () {
		try {
			int nb = 0;
			
			ResultSet rs = DBService.getInstance().doQuery("SELECT * FROM npc_vendor");
			while (rs.next()) {
				int ShopID = rs.getInt("npcid");
				ShopName[ShopID] = rs.getString("name");
				ShopSModifier[ShopID] = rs.getInt("sell");
				ShopBModifier[ShopID] = rs.getInt("buy");
				TotalShops++;
				nb++;
			}
			
			ResultSet rs2 = DBService.getInstance().doQuery("SELECT * FROM item_vendor");
			
			while (rs2.next()) {
				int ShopID = rs2.getInt("npcid");
				ShopItems[ShopID][ShopItemsStandard[ShopID]] = rs2.getInt("item") + 1;
				ShopItemsN[ShopID][ShopItemsStandard[ShopID]] = rs2.getInt("nb");
				ShopItemsSN[ShopID][ShopItemsStandard[ShopID]] = rs2.getInt("nb");
				ShopItemsStandard[ShopID]++;
			}
			LogService.getInstance().logInfo("npc_vendor load ("+nb+")");
			LogService.getInstance().logInfo("item_vendor load");
			return true;
		}
		catch (SQLException ex) {
			LogService.getInstance().logError("Can't load item_vendor or npc_vendor");
			LogService.getInstance().logError(ex.getMessage());
		}
		return false;
	}
	
	public int rShopItems (int a, int b) {
		return ShopItems[a][b];
	}
	
	public void cShopItems (int a, int b, int c) {
		ShopItems[a][b] = c;
	}
	
	public int rShopItemsN (int a, int b) {
		return ShopItemsN[a][b];
	}
	
	public void cShopItemsN (int a, int b, int c) {
		ShopItemsN[a][b] = c;
	}
	
	public void aShopItemsN (int a, int b, int c) {
		ShopItemsN[a][b] += c;
	}
	
	public void cShopItemsDelay (int a, int b, int c) {
		ShopItemsDelay[a][b] = c;
	}
	
	public int rShopItemsStandard (int a) {
		return ShopItemsStandard[a];
	}
	
	public String rShopName (int a) {
		return ShopName[a];
	}
	
	public int rShopSModifier (int a) {
		return ShopSModifier[a];
	}
	
	public int rShopBModifier (int a) {
		return ShopBModifier[a];
	}
	
	public int rMaxShopItems () {
		return MaxShopItems;
	}
}
